require 'rubygems'
gem 'sequel'
gem 'json'

require 'sequel'
require 'json'

require 'pp'

class Sequel::Database
	def schema_json(table, excludeColumns)
		tableSchema = schema(table.to_sym)
		jsonSchema = {}
		jsonSchema["name"] = table.to_s
		jsonProperties = {}
		tableSchema.each do |cschema|
			jumpNext = false
			excludeColumns.each do |ec|
				if (ec.kind_of? String)
					if (ec.to_s==cschema[0].to_s)
						jumpNext = true
					end
				elsif (ec.kind_of? Regexp)
					if (ec=~cschema[0].to_s)
						jumpNext = true
					end				
				else
					if (ec.to_s.to_sym==cschema[0].to_s.to_sym)
						jumpNext = true
					end
				end
			end
			if (jumpNext)
				next
			end
			jsonColumnSchema = {}
			jsonColumnSchema["type"] = cschema[1][:type]
			jsonColumnSchema["required"] = cschema[1][:allow_null]
			jsonColumnSchema["default"] = cschema[1][:default]
			jsonProperties[cschema[0].to_s] = jsonColumnSchema
		end
		jsonSchema["properties"] = jsonProperties
		JSON.generate(jsonSchema)
	end
end

